package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.SelfMessageList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.MySelfAdapter;
import com.lsy.weizhi2.adapter.UnReadAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.fragment.ReadFragment;
import com.lsy.weizhi2.fragment.UnReadFragment;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MySelfMessage extends MyBaseActivity {
    private Context context;
    private MyTitleView myself_title;
    private RecyclerView myself_rv;
    private UnReadAdapter mySelfAdapter;
    private List<SelfMessageList.ListBean> list;
    private int pageNo;
    private Gson gson;
    private boolean bottom;
    private int xx;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_self_message);
        context = MySelfMessage.this;
        Init();
    }

    private void Init() {
        myself_title = (MyTitleView) findViewById(R.id.myself_title);
        myself_rv = (RecyclerView) findViewById(R.id.myself_rv);
        mySelfAdapter = new UnReadAdapter(context, null);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        myself_rv.setLayoutManager(manager);
        myself_rv.setAdapter(mySelfAdapter);
        gson = new Gson();
        list = new ArrayList<>();
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = myself_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(myself_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                myself_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
        myself_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                boolean islast=myself_rv.canScrollVertically(1);
                if(newState==RecyclerView.SCROLL_STATE_DRAGGING&&!islast&&!bottom){
                    Log.i("Home_Hot","size==="+list.size());
                    if(list.size()==(pageNo)*10){
                        mySelfAdapter.UpData(list,1,1);
                    }else{
                        mySelfAdapter.UpData(list,1,3);
                    }
                    bottom=true;
                }else if(bottom&&newState==RecyclerView.SCROLL_STATE_IDLE&&!islast){
                    if(list.size()==(pageNo)*10){
                        mySelfAdapter.UpData(list,1,2);
                        pageNo++;
                        Log.i("Home_Hot","pageNo==="+pageNo);
                        getData();
                    }else{
                        mySelfAdapter.UpData(list,1,3);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mySelfAdapter.UpData(list,0,0);
                            }
                        },1000);
                    }
                    bottom=false;
                }
            }
        });
        pageNo=1;
        getData();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
            }
        },60000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        xx = 0;
    }

    private void getData(){
        Map<String,String> map=new HashMap<>();
        map.put("pageNo",pageNo+"");
        map.put("pageSize","10");
        MyOkHttpUtils.getInstance(context).doGet(Constants.SendPrivateMessage,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ReadFragment","message==="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){
                                    if(pageNo==1){
                                        list.clear();
                                    }
                                    SelfMessageList selfMessageList=gson.fromJson(message,SelfMessageList.class);
                                    list.addAll(selfMessageList.getList());
                                    mySelfAdapter.UpData(list,0,0);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

}
